package com.whut.transportation.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.whut.transportation.entity.Reason;
import com.whut.transportation.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ReasonAdapter extends BaseAdapter{
	private List<Reason> reasons;
	private int resource;//绑定的条目界面
	private LayoutInflater inflater;//布局填充器
	public static Map<Integer, Boolean> isSelected;  
	
/*	public ReasonAdapter(Context context,List<Reason> reasons, int resource) {
		this.reasons = reasons;
		this.resource = resource;
		this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }  
	*/
	public ReasonAdapter(Context context,List<Reason> reasons) {
		this.reasons = reasons;
		this.inflater =  LayoutInflater.from(context); 
		init();
    } 
	  //初始化  
    private void init() {  
        isSelected = new HashMap<Integer, Boolean>();  
        for (int i = 0; i < reasons.size(); i++) {  
            isSelected.put(i, false);  
        }  
    }  

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return reasons.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return reasons.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder holder = null;  
		if(convertView==null){
			//convertView = inflater.inflate(resource, null);
			  holder = new ViewHolder();  
	            convertView = inflater.inflate(R.layout.reason_list, null);  
	            holder.title = (TextView) convertView.findViewById(R.id.title);  
	            holder.cBox = (CheckBox) convertView.findViewById(R.id.cb);  
	            convertView.setTag(holder);  
		}else {  
            holder = (ViewHolder) convertView.getTag();  
        }  
		Reason reason = reasons.get(position);
        holder.title.setText(reason.getDescription());  
        holder.cBox.setChecked(isSelected.get(position));  
		
		/*CheckBox checkBox1 = (CheckBox) convertView.findViewById(R.id.checkBox1);
		CheckBox checkBox2 = (CheckBox) convertView.findViewById(R.id.checkBox2);
		Reason reason = reasons.get(position);
		int flag = 1;
		if(reason.getType()==0)
			flag = 0;
		else 
			flag = 1;
		reason.setType(flag);
		checkBox1.setText(reason.getDescription());
		checkBox2.setText(reason.getDescription());*/
		return convertView;
	}
	
	 public final class ViewHolder {  
	        public TextView title;  
	        public CheckBox cBox;  
	    }  
}
